package com.hnas.activity.web;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnas.activity.dto.DTOFindAllRankReturn;
import com.hnas.activity.dto.DTOGetInvitationCodeForm;
import com.hnas.activity.dto.DTOGetInvitationCodeReceive;
import com.hnas.activity.dto.DTOGetInvitationForm;
import com.hnas.activity.dto.DTOInviRank;
import com.hnas.api.dto.DTOResult;
import com.hnas.api.server.PostConnServer;
import com.hnas.appLogin.dto.search.WxElements;
import com.hnas.common.interfacedsc.InterfaceDsc;
import com.hnas.common.util.CommonReturn;
import com.hnas.common.util.CommonUtils;
import com.hnas.common.util.PostServerInfoGet;
import com.hnas.common.util.ResultCode;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;

@Controller
@RequestMapping("/activity/invitation")
public class InvitationController {
	
	@Autowired
	private PostConnServer postConnServer;
	
	@Autowired 
	private PostServerInfoGet postServerInfoGet;
	
	private static Log log = LogFactory.getLog(InvitationController.class);

	private static ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * 获取邀请码
	 * @param request
	 * @return
	 */
	@RequestMapping("/getInvitationCode")
	@ResponseBody
	public String getInvitionCode(HttpServletRequest request){
		 DTOResult dtoResult = new DTOResult();
			String sendData = "";
			try {
				sendData = request.getParameter("sendData");
				// 入参为空判断
				if (StringUtil.isEmpty(sendData)) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_10000, null);
				}
				JSONObject jo = (JSONObject) JSON.parse(sendData);
				String invitationId = jo.getString("accountId");
				String activityId = jo.getString("activityId");
				String wx_name = jo.getString("wx_name");
				String wx_image = jo.getString("wx_image");
				if (StringUtil.isEmpty(invitationId)) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_001, null);
				}
				
				if (StringUtil.isEmpty(activityId)) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_002, null);
				}
				
				//微信名称加密
				if (StringUtil.isNotEmpty(wx_name)) {
					wx_name = URLEncoder.encode(wx_name, "utf-8");
				}
				//编辑请求参数
				DTOGetInvitationCodeForm form = new DTOGetInvitationCodeForm();
				form.setActivityId(activityId);
				form.setInvitationId(invitationId);
				WxElements wxElements = new WxElements();
				wxElements.setWx_image(wx_image);
				wxElements.setWx_name(wx_name);
				form.setElements(objectMapper.writeValueAsString(wxElements));
				//发送请求获取邀请码
				DTOGetInvitationCodeReceive dto = null;
				//DTOGetInvitationCodeReceive dto = postConnServer.getResultData(form, "Activity_Invitation_GetInvitionCode", DTOGetInvitationCodeReceive.class);
				CommonReturn commonReturn = postServerInfoGet.getInvitationCode(form);
				if(ResultCode.SUCCESS.equals(commonReturn.getResultCode())){
					dto = (DTOGetInvitationCodeReceive) commonReturn.getResultData();
					return CommonUtils.createResult(dtoResult, ResultCode.SUCCESS,objectMapper.writeValueAsString(dto));
				}else{
					return objectMapper.writeValueAsString(commonReturn);
				}
				
				
			} catch (Exception e) {
				e.printStackTrace();
				// 日志记录
				log.info(DateUtil.dateTimeToString(new Date()) + "调用接口：" + InterfaceDsc.APP_Activity_getCode
						+ ",请求数据:入参为:" + sendData + "  出错:" + e);
				return CommonUtils.createResult(dtoResult, ResultCode.FAIL, null);
			} finally {
				// 日志记录
				log.info(DateUtil.dateTimeToString(new Date()) + "调用接口：" + InterfaceDsc.APP_Activity_getCode
						+ ",请求数据:入参为:" + sendData + "  返回参数:" + dtoResult);
			}
	 }
	
	
	/**
	 * 查询排行榜
	 * @param request
	 * @return
	 */
	@RequestMapping("/getInvitationRanking")
	@ResponseBody
	public String getInvitationRanking(HttpServletRequest request){
		 DTOResult dtoResult = new DTOResult();
			String sendData = "";
			try {
				sendData = request.getParameter("sendData");
				// 入参为空判断
				if (StringUtil.isEmpty(sendData)) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_10000, null);
				}
				JSONObject jo = (JSONObject) JSON.parse(sendData);
				String invitationId = jo.getString("accountId");
				String activityId = jo.getString("activityId");
				String pageSize = jo.getString("pageSize");
				if (StringUtil.isEmpty(invitationId)) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_001, null);
				}
				
				if (StringUtil.isEmpty(activityId)) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_002, null);
				}
				
				DTOFindAllRankReturn dto = new DTOFindAllRankReturn();
				DTOGetInvitationForm form = new DTOGetInvitationForm();
				form.setActivityId(activityId);
				form.setInvitationId(invitationId);
				form.setPageSize(pageSize);
				form.setPageNo("1");
                List<DTOInviRank> list = null;
                try {
                	CommonReturn commonReturn = postServerInfoGet.getInvitationRanking(form);
    				if(ResultCode.SUCCESS.equals(commonReturn.getResultCode())){
    					list = (List<DTOInviRank>) commonReturn.getResultData();
    					if(null!=list&&list.size()>0){
    						dto.setList(list);
    					}
    				}else{
    					return objectMapper.writeValueAsString(commonReturn);
    				}
    				DTOGetInvitationCodeForm form1 = new DTOGetInvitationCodeForm();
    				form1.setActivityId(activityId);
    				form1.setInvitationId(invitationId);
    				DTOGetInvitationCodeReceive dto1 = postConnServer.getResultData(form1, "Activity_Invitation_GetInvitionCode", DTOGetInvitationCodeReceive.class);
    				if(null == dto1){
    					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_138, null);
    				}
    				dto.setRank(dto1.getRanking());
    				dto.setTotal(dto1.getInvitationTotal());
				} catch (Exception e) {
					e.printStackTrace();
					log.info("请求活动接口出错！");
					dto.setList(new ArrayList<DTOInviRank>());
					dto.setRank("0");
    				dto.setTotal("0");
				}
				
				return CommonUtils.createResult(dtoResult, ResultCode.SUCCESS,objectMapper.writeValueAsString(dto));
			} catch (Exception e) {
				e.printStackTrace();
				// 日志记录
				log.info("调用接口：" + InterfaceDsc.APP_Activity_getInvitationRanking
						+ ",请求数据:入参为:" + sendData + "  出错:" + e);
				return CommonUtils.createResult(dtoResult, ResultCode.FAIL, null);
			} finally {
				// 日志记录
				log.info("调用接口：" + InterfaceDsc.APP_Activity_getInvitationRanking
						+ ",请求数据:入参为:" + sendData + "  返回参数:" + dtoResult);
			}
	 }
	
	/*public static void main(String[] args) {
		List<DTOInviRank> list = new ArrayList<DTOInviRank>();
		DTOInviRank r1 = new DTOInviRank();
		r1.setRanking("1");
		r1.setTotal("6");
		DTOInviRank r2 = new DTOInviRank();
		r2.setRanking("2");
		r2.setTotal("6");
		DTOInviRank r3 = new DTOInviRank();
		r3.setRanking("3");
		r3.setTotal("5");
		DTOInviRank r4 = new DTOInviRank();
		r4.setRanking("4");
		r4.setTotal("5");
		DTOInviRank r5 = new DTOInviRank();
		r5.setRanking("5");
		r5.setTotal("2");
		list.add(r1);
		list.add(r2);
		list.add(r3);
		list.add(r4);
		list.add(r5);
		System.out.println("排序前：");
		for(DTOInviRank r :list){
			System.out.println(r.getRanking()+","+r.getTotal());
		}
		int num = 1;
		for(int i=1;i<list.size();i++){
			if(list.get(i-1).getTotal().equals(list.get(i).getTotal())){
				list.get(i).setRanking(""+num);
			}else{
				num= num + i;
				list.get(i).setRanking(""+(i+1));
			}
		}
		
		System.out.println("排序后：");
		for(DTOInviRank r :list){
			System.out.println(r.getRanking()+","+r.getTotal());
		}
		
		
	}*/
	
}
